<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <form id="myForm">
    <label for="name">姓名：</label>
    <input type="text" id="name" name="name"><br>
    <label for="age">年龄：</label>
    <input type="text" id="age" name="age"><br>
    <input type="button" value="提交" onclick="submitForm()">
  </form>
</body>
</html>

    <script>
const http = require('http');
const url = require('url');
const mysql = require('mysql2');

// 创建连接池
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'test',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
});

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
  
  const query = url.parse(req.url, true).query;

  // 处理数据库操作
  pool.getConnection((err, connection) => {
    if (err) throw err;
      
    // 插入数据
    const sql = 'INSERT INTO users SET ?';
    const data = { name: query.name, age: query.age };
    pool.query(sql, data, (err, results, fields) => {
      if (err) throw err;
      
      console.log(`插入 ${results.affectedRows} 行数据`);
      
      res.write('数据已保存到数据库！', 'utf-8');
      res.end();
    });

    // 释放连接
    connection.release();
  });
});

server.listen(3000, () => {
  console.log('服务器已启动，正在监听端口 3000...');
});


    </script>
</body>
</html>